package com.ZeroCarbon.domain.dto;

import cn.hutool.core.bean.BeanUtil;
import com.ZeroCarbon.domain.po.Post;
import com.ZeroCarbon.utils.BusinessMethodUtils;
import com.ZeroCarbon.utils.JsonUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;

import static com.ZeroCarbon.prefix.FilePrefix.POST_IMAGES;

/**
 * 记录类
 *
 * @since 2024-10-26
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PostReceiverDTO {
    /**
     * 记录id
     */
    private String id;

    /**
     * 标题
     */
    private String title;

    /**
     * 正文
     */
    private String content;

    /**
     * 图片
     */
    private List<MultipartFile> images;

    /**
     * 标签信息
     */
    private List<String> tags;

    /**
     * 是否私密
     */
    private String isPrivate;

    /**
     * PostReceiverDTO转PO
     */
    public Post toPost() {
        Post post = BeanUtil.copyProperties(this, Post.class);
        List<MultipartFile> images = this.images;
        //将图片生成的文件名加入集合
        List<String> filesList = BusinessMethodUtils.generateFilesList(POST_IMAGES, images);
        //设置信息
        return post.setImagesJson(JsonUtils.ObjectToJson(filesList))
                .setTagsJson(JsonUtils.ObjectToJson(this.tags))
                .setLastTime(LocalDateTime.now());
    }
}
